package com.qikux.service.impl;

import com.qikux.dao.ResourceDao;
import com.qikux.dto.ResourceDTO;
import com.qikux.entity.Resource;
import com.qikux.entity.ResourceType;
import com.qikux.service.ResourceService;
import com.qikux.vo.PageVo;
import com.qikux.vo.R;
import org.haredot.annotation.Service;
import org.haredot.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {

    @org.haredot.annotation.Resource
    private ResourceDao resourceDao ;

    @Override
    public R<List<ResourceType>> queryTypes() {

        List<ResourceType> list = resourceDao.findTypes();

        return R.<List<ResourceType>>ok().data(list);
    }

    @Override
    public R<?> saveResource(Resource resource) {

        int len = resourceDao.saveResource(resource) ;

        return R.ok().message("资源上传成功,我们会在3个工作日内对您上传的资源进行审核,请耐心等待.");
    }

    @Override
    public R<PageVo<ResourceDTO>> queryResourceByType(Long typeId, PageVo<ResourceDTO> pageVo) {
        // DTO 数据传输对象    entity (实体层， 和数据库的字段一一对应) , DTO 解决 实体层 字段缺失问题 。
        List<ResourceDTO> resources = resourceDao.findResourceByTypeId(typeId, pageVo) ;

        // 获取 typeId 查询到的所有数据的 总条数
        long total = resourceDao.findResourceByTypeIdCount(typeId);

        pageVo.setTotal(total);
        pageVo.setList(resources);

        return R.<PageVo<ResourceDTO>>ok().data(pageVo);
    }
}
